package com.module.codeBind.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;
import base.util.Page;
import base.util.StringUtil;
import base.util.transaction.TransactionCallback;
import base.util.transaction.TransactionTemplate;

import com.module.codeBind.dao.CodeAppDao;
import com.module.codeBind.dao.CodeBindDao;
import com.module.codeBind.vo.CodeBind;

/**
 * 
 * 码绑定操作服务类
 * 
 * @author dengsq
 * @version [V1.0, Jul 21, 2011]
 * @see [相关类CodeBindDao/方法]
 * @since [二代产品/码绑定]
 */
public class CodeBindManager extends GenericHibernateManager<CodeBind, String> {
	private CodeBindDao dao = CodeBindDao.getInstance();
	private static CodeBindManager service;

	CodeBindManager() {
	}

	public static CodeBindManager getInstance() {
		if (service == null) {
			service = new CodeBindManager();
		}
		return service;
	}

	public Map<String, Object> saveCodeBind(final CodeBind bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtil.isNull(bean.getId())) {
			bean.setUpdateTime(new Date());
			bean.setStatus(CUDEnum.U.toString());
		} else {
			bean.setCreateTime(new Date());
			bean.setStatus(CUDEnum.C.toString());
			bean.setBindTime(new Date());
		}
		final Session session = HibernateSessionFactory.getSession();
		final TransactionTemplate template = new TransactionTemplate(session);
		template.getJsonMsg().setMsg("保存成功");// \u4fdd\u5b58\u6210\u529f

		template.execute(new TransactionCallback() {
			@Override
			public void doInTransaction() {
				boolean canSave = dao.verifyBatchNoIsRepeat(session, bean);
				if (!canSave) {
					template.getJsonMsg().setFailMsg("操作失败，当前批次已绑定，请重新选择");
				} else {
					dao.saveCodeBind(session, bean);
				}
			}
		});
		map = template.getJsonMsg().getMap();
		return map;
	}

	/**
	 * 删除 码绑定
	 * 
	 * @param obj
	 * @return
	 */
	public Map<String, Object> delCodeBind(final CodeBind obj) {
		Map<String, Object> map = new HashMap<String, Object>();

		final Session session = HibernateSessionFactory.getSession();
		TransactionTemplate template = new TransactionTemplate(session);
		template.getJsonMsg().setMsg("\u5220\u9664\u6210\u529f");// 删除成功
		template.execute(new TransactionCallback() {
			@Override
			public void doInTransaction() {
				dao.deleteCodeBind(session, obj);
			}
		});
		map = template.getJsonMsg().getMap();
		return map;

	}

	/**
	 * 分页查询码绑定信息
	 * 
	 * @param beanUtil
	 *            公共实体对象
	 * @return Page 分页对象
	 * @throws Exception
	 */
	public Page toList(CodeBind codeBind, Page page) throws Exception {
		Session session = HibernateSessionFactory.getSession();
		try {
			page = dao.toList(session, codeBind, page.getStart(),
					page.getLimit());
			List<Object> list = page.getRows();
			List<Object> codeBindList = new ArrayList<Object>();
			if (!list.isEmpty()) {
				for (int i = 0; i < list.size(); i++) {
					CodeBind bean = (CodeBind) list.get(i);
					codeBindList.add(bean);
				}
			}
			page.setRows(codeBindList);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}

	/**
	 * 根据ID查询对象
	 * 
	 * @param id
	 *            码绑定对象ID
	 * @throws Exception
	 */
	public CodeBind get(String id) throws Exception {
		Session session = HibernateSessionFactory.getSession();
		CodeBind bean = null;
		try {
			bean = dao.findById(session, id);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return bean;
	}

	/**
	 * 根据码获取商品ID
	 * 
	 * @param code
	 *            真知码
	 * @return 返回商品ID
	 * @throws Exception
	 */
	public String getProductByCode(String code) throws Exception {
		Session session = HibernateSessionFactory.getSession();
		String str = null;
		try {
			str = dao.getProductByCode(session, code);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return str;
	}

	@Override
	public GenericHibernateDao<CodeBind, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
}
